#!/usr/bin/env python3
"""Debug Java parsing to understand why blocks are missing"""

from repo_index.codeblocks.parser.create import create_parser

java_code = """
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
"""

print("Debugging Java parsing...")
print("=" * 50)

# Create parser with debug mode
parser = create_parser("java", debug=True)
print(f"Parser type: {type(parser)}")
print(f"Parser queries count: {len(parser.queries) if hasattr(parser, 'queries') else 'N/A'}")

# Parse the Java code
module = parser.parse(java_code.strip(), file_path="HelloWorld.java")

print("\n" + "=" * 50)
print("Parsed structure:")

def print_full_structure(block, indent=0):
    prefix = "  " * indent
    identifier = f" '{block.identifier}'" if block.identifier else ""
    print(f"{prefix}[{block.type.value}]{identifier}")
    print(f"{prefix}  - Tokens: {block.tokens}")
    print(f"{prefix}  - Content length: {len(block.content) if block.content else 0}")
    print(f"{prefix}  - Children: {len(block.children) if hasattr(block, 'children') else 0}")

    if block.content and len(block.content) > 0:
        preview = block.content[:50].replace('\n', '\\n')
        print(f"{prefix}  - Content preview: {preview}...")

    if hasattr(block, 'children'):
        for child in block.children:
            print_full_structure(child, indent + 1)

print_full_structure(module)

# Check what queries are loaded
print("\n" + "=" * 50)
print("Parser queries:")
if hasattr(parser, 'queries'):
    for i, (label, node_type, _) in enumerate(parser.queries[:5]):  # Show first 5
        print(f"  {i+1}. {label} -> {node_type}")